﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 样式表
    /// </summary>
    /// <remarks>对应 SketchUpAPI 中的 SUStylesRef </remarks>
    public class StyleTable : ObjectBase
    {
        #region API 函数
        /// <summary>
        /// 获取样式个数
        /// </summary>
        /// <param name="styles"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUStylesGetNumStyles(IntPtr styles, out long count);

        /// <summary>
        /// 获取所有样式
        /// </summary>
        /// <param name="styles"></param>
        /// <param name="len"></param>
        /// <param name="styleArray"></param>
        /// <param name="returnCount"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUStylesGetStyles(IntPtr styles, long len, IntPtr[] styleArray, out long returnCount);

        /// <summary>
        /// 添加一个样式
        /// </summary>
        /// <param name="stylesRef"></param>
        /// <param name="path">样式文件</param>
        /// <param name="activate"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUStylesAddStyle(IntPtr stylesRef, [MarshalAs(UnmanagedType.LPUTF8Str)] string path, bool activate);

        /// <summary>
        /// 获取当前样式
        /// </summary>
        /// <param name="styles"></param>
        /// <param name="style"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUStylesGetActiveStyle(IntPtr styles, out IntPtr style);
        #endregion

        /// <summary>
        /// 当前样式
        /// </summary>
        public Style ActiveStyle
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUStylesGetActiveStyle(Id, out IntPtr id));
                return new Style(id);
            }
        }

        /// <summary>
        /// 样式个数
        /// </summary>
        public int Count
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUStylesGetNumStyles(Id, out long count));
                return (int)count;
            }
        }

        /// <summary>
        /// 所有样式
        /// </summary>
        public List<Style> Styles
        {
            get
            {
                IntPtr[] intPtrs = new IntPtr[Count];
                SketchUpExceptionHelper.ThrowSUResult(SUStylesGetStyles(Id, Count, intPtrs, out long returnCount));

                List<Style> styles = new List<Style>((int)returnCount);
                for (int i = 0; i < returnCount; i++)
                {
                    styles.Add(new Style(intPtrs[i]));
                }

                return styles;
            }
        }

        internal StyleTable(IntPtr id)
        {
            Id = id;
        }

        /// <summary>
        /// 添加一个样式
        /// </summary>
        /// <param name="path">样式文件</param>
        /// <param name="active">是否激活</param>
        public void AddStyle(string path, bool active)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUStylesAddStyle(Id, path, active));
        }
    }
}